package cn.dengta.webapp.trade.entity;

import java.math.BigDecimal;
import java.util.Date;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import cn.dengta.common.model.Status;
import cn.dengta.common.util.Common;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import cn.dengta.webapp.trade.model.*;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.StringUtil;
import me.codeplayer.util.X;

/**
 * 积分收益日统计记录表
 *
 * @author lgy
 * @since 2022-12-09
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("tr_user_income_daily_log")
public class UserIncomeDailyLog extends TimeBasedEntity {

	/** 用户ID */
	Long userId;
	/** 总收益 */
	BigDecimal totalIntegral;
	/** 总收益（推广员） */
	BigDecimal agentGainIntegral;
	/** 搭讪收益 */
	BigDecimal greetIntegral;
	/** 聊天收益 */
	BigDecimal msgIntegral;
	/** 语音收益 */
	BigDecimal audioIntegral;
	/** 视频收益 */
	BigDecimal videoIntegral;
	/** 礼物收益 */
	BigDecimal giftIntegral;
	/** 直播礼物收益 */
	BigDecimal giftLiveIntegral;
	/** 背包礼物收益 */
	BigDecimal giftBagIntegral;
	/** 打赏收益 */
	BigDecimal donateIntegral;
	/** 领红包收益 */
	BigDecimal redPacketIntegral;
	/** 用户分成收益（工会） */
	BigDecimal extractUserIntegral;
	/** 推广员积分收益（工会） */
	BigDecimal agentRewardIntegral;
	/** 主播积分奖励收益（奖励） */
	BigDecimal userRewardIntegral;
	/** 速配积分奖励收益（奖励） */
	BigDecimal speedDatingRewardIntegral;
	/** 邀请积分奖励收益（奖励） */
	BigDecimal inviteGainIntegral;
	/** 主播考核积分收益（奖励） */
	BigDecimal rewardAssessIntegral;
	/** 总消耗钻石（推广员） */
	Integer agentCoin;
	/** 总消耗钻石（含免费钻） */
	Integer totalCoin;
	/** 总消耗免费钻 */
	Integer totalFreeCoin;
	/** 搭讪总钻石 */
	Integer greetCoin;
	/** 搭讪免费钻石 */
	Integer greetFreeCoin;
	/** 聊天总钻石 */
	Integer msgCoin;
	/** 聊天消耗免费钻 */
	Integer msgFreeCoin;
	/** 语音总钻石 */
	Integer audioCoin;
	/** 语音消耗免费钻 */
	Integer audioFreeCoin;
	/** 视频总钻石 */
	Integer videoCoin;
	/** 视频消耗免费钻 */
	Integer videoFreeCoin;
	/** 礼物总钻石 */
	Integer giftCoin;
	/** 礼物消耗免费钻 */
	Integer giftFreeCoin;
	/** 直播礼物消耗钻 */
	Integer giftLiveCoin;
	/** 直播礼物消耗免费钻 */
	Integer giftLiveFreeCoin;
	/** 打赏总钻石 */
	Integer donateCoin;
	/** 打赏消耗免费钻 */
	Integer donateFreeCoin;
	/** 父级推广员ID */
	Long rootAgentId;
	/** 当前推广员ID */
	Long agentId;
	/** 统计时间 */
	Date reportTime;
	Integer status;

	public UserIncomeDailyLog init(Date reportTime, UserIncomeDailyLog log, Date now) {
		this.userId = log.userId;
		this.totalIntegral = X.expectNotNull(log.totalIntegral, BigDecimal.ZERO);
		this.agentGainIntegral = X.expectNotNull(log.agentGainIntegral, BigDecimal.ZERO);
		this.greetIntegral = X.expectNotNull(log.greetIntegral, BigDecimal.ZERO);
		this.msgIntegral = X.expectNotNull(log.msgIntegral, BigDecimal.ZERO);
		this.audioIntegral = X.expectNotNull(log.audioIntegral, BigDecimal.ZERO);
		this.videoIntegral = X.expectNotNull(log.videoIntegral, BigDecimal.ZERO);
		this.giftIntegral = X.expectNotNull(log.giftIntegral, BigDecimal.ZERO);
		this.giftLiveIntegral = X.expectNotNull(log.giftLiveIntegral, BigDecimal.ZERO);
		this.giftBagIntegral = X.expectNotNull(log.giftBagIntegral, BigDecimal.ZERO);
		this.donateIntegral = X.expectNotNull(log.donateIntegral, BigDecimal.ZERO);
		this.redPacketIntegral = X.expectNotNull(log.redPacketIntegral, BigDecimal.ZERO);
		this.extractUserIntegral = X.expectNotNull(log.extractUserIntegral, BigDecimal.ZERO);
		this.agentRewardIntegral = X.expectNotNull(log.agentRewardIntegral, BigDecimal.ZERO);
		this.userRewardIntegral = X.expectNotNull(log.userRewardIntegral, BigDecimal.ZERO);
		this.speedDatingRewardIntegral = X.expectNotNull(log.speedDatingRewardIntegral, BigDecimal.ZERO);
		this.inviteGainIntegral = X.expectNotNull(log.inviteGainIntegral, BigDecimal.ZERO);
		this.rewardAssessIntegral = X.expectNotNull(log.rewardAssessIntegral, BigDecimal.ZERO);
		this.agentCoin = X.expectNotNull(log.agentCoin, 0);
		this.totalCoin = X.expectNotNull(log.totalCoin, 0);
		this.totalFreeCoin = X.expectNotNull(log.totalFreeCoin, 0);
		this.greetCoin = X.expectNotNull(log.greetCoin, 0);
		this.greetFreeCoin = X.expectNotNull(log.greetFreeCoin, 0);
		this.msgCoin = X.expectNotNull(log.msgCoin, 0);
		this.msgFreeCoin = X.expectNotNull(log.msgFreeCoin, 0);
		this.audioCoin = X.expectNotNull(log.audioCoin, 0);
		this.audioFreeCoin = X.expectNotNull(log.audioFreeCoin, 0);
		this.videoCoin = X.expectNotNull(log.videoCoin, 0);
		this.videoFreeCoin = X.expectNotNull(log.videoFreeCoin, 0);
		this.giftCoin = X.expectNotNull(log.giftCoin, 0);
		this.giftFreeCoin = X.expectNotNull(log.giftFreeCoin, 0);
		this.giftLiveCoin = X.expectNotNull(log.giftLiveCoin, 0);
		this.giftLiveFreeCoin = X.expectNotNull(log.giftLiveFreeCoin, 0);
		this.donateCoin = X.expectNotNull(log.donateCoin, 0);
		this.donateFreeCoin = X.expectNotNull(log.donateFreeCoin, 0);
		this.rootAgentId = log.rootAgentId;
		this.agentId = log.agentId;
		this.reportTime = reportTime;
		this.status = X.expectNotNull(log.status, status, Status.YES.value);
		super.initTime(now);
		return this;
	}

	public UserIncomeDailyLog init() {
		this.totalIntegral = BigDecimal.ZERO;
		this.agentGainIntegral = BigDecimal.ZERO;
		this.greetIntegral = BigDecimal.ZERO;
		this.msgIntegral = BigDecimal.ZERO;
		this.audioIntegral = BigDecimal.ZERO;
		this.videoIntegral = BigDecimal.ZERO;
		this.giftIntegral = BigDecimal.ZERO;
		this.giftLiveIntegral = BigDecimal.ZERO;
		this.giftBagIntegral = BigDecimal.ZERO;
		this.donateIntegral = BigDecimal.ZERO;
		this.redPacketIntegral = BigDecimal.ZERO;
		this.extractUserIntegral = BigDecimal.ZERO;
		this.agentRewardIntegral = BigDecimal.ZERO;
		this.userRewardIntegral = BigDecimal.ZERO;
		this.speedDatingRewardIntegral = BigDecimal.ZERO;
		this.inviteGainIntegral = BigDecimal.ZERO;
		this.rewardAssessIntegral = BigDecimal.ZERO;
		this.totalCoin = 0;
		this.agentCoin = 0;
		this.totalFreeCoin = 0;
		this.greetCoin = 0;
		this.greetFreeCoin = 0;
		this.msgCoin = 0;
		this.msgFreeCoin = 0;
		this.audioCoin = 0;
		this.audioFreeCoin = 0;
		this.videoCoin = 0;
		this.videoFreeCoin = 0;
		this.giftCoin = 0;
		this.giftFreeCoin = 0;
		this.giftLiveCoin = 0;
		this.giftLiveFreeCoin = 0;
		this.donateCoin = 0;
		this.donateFreeCoin = 0;
		return this;
	}

	public UserIncomeDailyLog add(@Nonnull UserIntegralLog log, @Nullable UserCoinLog coinLog) {
		if (log.getTradeAmount().compareTo(BigDecimal.ZERO) > 0 && Flow.INCOME.eq(log.getFlow()) && log.getHandleStatus() >= HandleStatus.INIT.getValue()) {
			BigDecimal agentGain = BigDecimal.ZERO;
			int agentCoin = 0;
			final BigDecimal tradeAmount = log.getTradeAmount();
			final int coin = Math.abs(X.mapElse(coinLog, UserCoinLog::getTradeAmount, 0)), freeCoin = Math.abs(X.mapElse(coinLog, UserCoinLog::getTradeFreePart, 0));
			final TradeType tradeType = TradeType.of(log.getTradeType());
			if (TradeType.INTEGRAL_GAIN == tradeType && log.getCoinTradeType() != null) {
				switch (TradeType.of(log.getCoinTradeType())) {
					case COIN_FOR_GREET:
						this.greetIntegral = this.greetIntegral.add(tradeAmount);
						this.greetCoin += coin;
						this.greetFreeCoin += freeCoin;
						agentGain = tradeAmount;
						agentCoin = coin;
						break;
					case COIN_FOR_MSG:
						this.msgIntegral = this.msgIntegral.add(tradeAmount);
						this.msgCoin += coin;
						this.msgFreeCoin += freeCoin;
						agentGain = tradeAmount;
						agentCoin = coin;
						break;
					case COIN_FOR_AUDIO:
						this.audioIntegral = this.audioIntegral.add(tradeAmount);
						this.audioCoin += coin;
						this.audioFreeCoin += freeCoin;
						agentGain = tradeAmount;
						agentCoin = coin;
						break;
					case COIN_FOR_VIDEO:
						this.videoIntegral = this.videoIntegral.add(tradeAmount);
						this.videoCoin += coin;
						this.videoFreeCoin += freeCoin;
						agentGain = tradeAmount;
						agentCoin = coin;
						break;
					case COIN_FOR_GIFT:
						this.giftIntegral = this.giftIntegral.add(tradeAmount);
						this.giftCoin += coin;
						this.giftFreeCoin += freeCoin;
						agentGain = tradeAmount;
						agentCoin = coin;
						break;
					case COIN_FOR_GIFT_BAG:
						this.giftBagIntegral = this.giftBagIntegral.add(tradeAmount);
						agentGain = tradeAmount;
						break;
					case COIN_FOR_DONATE:
						this.donateIntegral = this.donateIntegral.add(tradeAmount);
						this.donateCoin += coin;
						this.donateFreeCoin += freeCoin;
						agentGain = tradeAmount;
						agentCoin = coin;
						break;
					case COIN_FOR_RED_PACKET:
						this.redPacketIntegral = this.redPacketIntegral.add(tradeAmount);
						agentGain = tradeAmount;
						break;
					case COIN_FOR_GIFT_LIVE:
						this.giftLiveIntegral = this.giftLiveIntegral.add(tradeAmount);
						this.giftLiveCoin += coin;
						this.giftLiveFreeCoin += freeCoin;
						agentGain = tradeAmount;
						agentCoin = coin;
						break;
					default:
				}
			} else if (TradeType.INTEGRAL_GAIN_EXTRACT_USER == tradeType) {
				this.extractUserIntegral = this.extractUserIntegral.add(tradeAmount);
			} else if (TradeType.INTEGRAL_REWARD_AGENT == tradeType) {
				this.agentRewardIntegral = this.agentRewardIntegral.add(tradeAmount);
			} else if (TradeType.INTEGRAL_REWARD_USER == tradeType) {
				this.userRewardIntegral = this.userRewardIntegral.add(tradeAmount);
			} else if (TradeType.INTEGRAL_REWARD_SPEED_DATING == tradeType) {
				this.speedDatingRewardIntegral = this.speedDatingRewardIntegral.add(tradeAmount);
			} else if (TradeType.INTEGRAL_INVITE_GAIN == tradeType) {
				this.inviteGainIntegral = this.inviteGainIntegral.add(tradeAmount);
			} else if (TradeType.INTEGRAL_REWARD_ASSESS == tradeType) {
				this.rewardAssessIntegral = this.rewardAssessIntegral.add(tradeAmount);
			}
			this.totalIntegral = this.totalIntegral.add(tradeAmount);
			if (agentGain.compareTo(BigDecimal.ZERO) > 0) {
				this.agentGainIntegral = this.agentGainIntegral.add(tradeAmount);
			}
			if (agentCoin > 0) {
				this.agentCoin += agentCoin;
			}
			this.totalCoin += coin;
			this.totalFreeCoin = freeCoin;
		}
		return this;
	}

	public String logKey() {
		return userId + Common.LINE_SEP + StringUtil.toString(agentId);
	}

	public static final String USER_ID = "user_id";
	public static final String TOTAL_INTEGRAL = "total_integral";
	public static final String AGENT_GAIN_INTEGRAL = "agent_gain_integral";
	public static final String GREET_INTEGRAL = "greet_integral";
	public static final String MSG_INTEGRAL = "msg_integral";
	public static final String AUDIO_INTEGRAL = "audio_integral";
	public static final String VIDEO_INTEGRAL = "video_integral";
	public static final String GIFT_INTEGRAL = "gift_integral";
	public static final String GIFT_LIVE_INTEGRAL = "gift_live_integral";
	public static final String GIFT_BAG_INTEGRAL = "gift_bag_integral";
	public static final String DONATE_INTEGRAL = "donate_integral";
	public static final String RED_PACKET_INTEGRAL = "red_packet_integral";
	public static final String EXTRACT_USER_INTEGRAL = "extract_user_integral";
	public static final String AGENT_REWARD_INTEGRAL = "agent_reward_integral";
	public static final String USER_REWARD_INTEGRAL = "user_reward_integral";
	public static final String SPEED_DATING_REWARD_INTEGRAL = "speed_dating_reward_integral";
	public static final String INVITE_GAIN_INTEGRAL = "invite_gain_integral";
	public static final String REWARD_ASSESS_INTEGRAL = "reward_assess_integral";
	public static final String AGENT_COIN = "agent_coin";
	public static final String TOTAL_COIN = "total_coin";
	public static final String TOTAL_FREE_COIN = "total_free_coin";
	public static final String GREET_COIN = "greet_coin";
	public static final String GREET_FREE_COIN = "greet_free_coin";
	public static final String MSG_COIN = "msg_coin";
	public static final String MSG_FREE_COIN = "msg_free_coin";
	public static final String AUDIO_COIN = "audio_coin";
	public static final String AUDIO_FREE_COIN = "audio_free_coin";
	public static final String VIDEO_COIN = "video_coin";
	public static final String VIDEO_FREE_COIN = "video_free_coin";
	public static final String GIFT_COIN = "gift_coin";
	public static final String GIFT_FREE_COIN = "gift_free_coin";
	public static final String GIFT_LIVE_COIN = "gift_live_coin";
	public static final String GIFT_LIVE_FREE_COIN = "gift_live_free_coin";
	public static final String DONATE_COIN = "donate_coin";
	public static final String DONATE_FREE_COIN = "donate_free_coin";
	public static final String ROOT_AGENT_ID = "root_agent_id";
	public static final String AGENT_ID = "agent_id";
	public static final String REPORT_TIME = "report_time";

}
